;(function() {
  'use strict';
  angular.module('controllers.omdisplay.successRate')
    .controller('srSuccessRateController', srSuccessRateController);

  function srSuccessRateController($scope, successRate, $timeout, CoreService, COREDATA,PublicFn) {
    var year, month, day;
    //帐期
    $scope.timeTypes = COREDATA.TIMETYPES;
    //季度
    $scope.quarters = COREDATA.QUARTERS;
    //省份
    $scope.provinces = COREDATA.PROVINCES;
    //业务线
    $scope.businessLine = COREDATA.BUSINESSLINE;
    //维度
    $scope.queryTypes = successRate.successRateData.getTypes();
    //页面绑定对象
    $scope.sendData = successRate.successRateData.getPageBindObj();
    //监控帐期，选择不同的帐期，给开始时间和结束时间不同的默认值
    year = new Date().getFullYear();
    month = new Date().getMonth();
    day = new Date().getDate() - 1;
    $scope.$watch('sendData.timeType', function(newValue) {
      switch (newValue) {
        case '5':
          $scope.sendData.startTime = year;
          $scope.sendData.endTime = year;
          break;
        case '4':
          $scope.sendData.startTime = '';
          $scope.sendData.endTime = '';
          break;
        case '3':
          $scope.sendData.startTime = new Date(year, month);
          $scope.sendData.endTime = new Date(year, month);
          break;
        case '1':
          $scope.sendData.startTime = new Date(year, month, day);
          $scope.sendData.endTime = new Date(year, month, day);
      }
    });
    $scope.btnDisabled = false;
    $scope.$on('formInvalid', function(event, data) {
      $scope.btnDisabled = data;
    });
    $scope.$watch('myForm.$invalid', function(newValue) {
      newValue ? $scope.btnDisabled = true : $scope.btnDisabled = false;
    });
    $scope._loading.show = false;
    $scope.send = {};
    $scope.search = function() {
      if ($scope.shoumun.num === 1) {
        $scope.shoumun.num = 1;
        return;
      }
      var send = successRate.successRateData.getSendObj($scope.sendData, $scope.businessLine);
      angular.copy(send, $scope.send);
      showQueryResult($scope.send);
      $scope.shoumun.num = 1;
      $scope._loading.show = true;
    };
    $scope.formPrefix = COREDATA.FORMPREFIX + 'Omdisplay/TransactionSuccessRate/successRate/exportExcel';

    var myEcharts1 = echarts.init(document.getElementById('doc1'));
    //显示查询结果,当省份为空时,x轴坐标为全部省份，否则以选择的省份为x轴坐标
    function showQueryResult(sendData) {
      //汇总对象
      $scope.gather = {};
      $scope.total = 0;
      var blName = '';
      sendData.businessLineName.length === 0 ? blName = '统一支付系统' : blName = sendData.businessLineName.toString();
      $scope.title = PublicFn.fmtDateTitle(sendData, 'timeType') + blName + '交易成功率';
      sendData.title = $scope.title;
      $scope.showProvince = {
        code: [],
        name: [],
        total: []
      };
      var pro = [];
      angular.forEach($scope.provinces, function(p) {
        pro.push(p.provinceName);
      });
      successRate.successRateServer.querySuccessRate(sendData, function(result) {
        if (result.state === '10000' && result.data.length >= 1) {
          angular.forEach(result.data, function(rd) {
            //后端返回的数据中没有queryTypeName
            if (rd.queryType === '01') {
              rd.queryTypeName = '全业务流程业务处理成功率';
            } else if (rd.queryType === '02') {
              rd.queryTypeName = '全业务流程系统处理成功率';
            } else if (rd.queryType === '03') {
              rd.queryTypeName = '省侧业务处理成功率';
            } else if (rd.queryType === '04') {
              rd.queryTypeName = '省侧系统处理成功率';
            } else if (rd.queryType === '05') {
              rd.queryTypeName = '统一支付业务处理成功率';
            }
            rd.successRateArrow = true;
          });
          $scope.items = result.data;
          $scope.itemArray = [];
          var obj = {};
          for (var i = 0; i < result.data.length; i++) {
            var resource = result.data[i];
            for (var j = 0; j < resource.lineData.length; j++) {
              var province = {
                provinceCode: resource.lineData[j].provinceCode,
                provinceName: resource.lineData[j].provinceName,
              };
              if (!obj[province.provinceName]) {
                obj[province.provinceName] = {
                  provinceCode: province.provinceCode,
                  provinceName: province.provinceName,
                  total: resource.lineData[j].total,
                  lineData: [{
                    queryTypeCode: resource.queryType,
                    volume: resource.lineData[j].volume,
                    failedAmount: resource.lineData[j].failedAmount,
                    processSuccessRate: resource.lineData[j].processSuccessRate
                  }]
                };
              } else {
                obj[province.provinceName].lineData.push({
                  queryTypeCode: resource.queryType,
                  volume: resource.lineData[j].volume,
                  failedAmount: resource.lineData[j].failedAmount,
                  processSuccessRate: resource.lineData[j].processSuccessRate
                });
              }
            }
          }
          for (var o in obj) {
            $scope.itemArray.push(obj[o]);
          }
          angular.forEach($scope.itemArray, function(t) {
            $scope.total += t.total;
          });

          $scope.gather = successRate.successRateFn.getSuccessRate(result);
          var a = successRate.successRateFn.getEchartsData($scope.itemArray, result.data, $scope.title);
          myEcharts1.clear();
          myEcharts1.setOption(a);
          $timeout(function() {
            $scope.outData = successRate.successRateFn.exportView(sendData, myEcharts1);
          });
          $scope.shoumun.num = 0;
          $scope.showArrow = true;
          $scope.flag = 'table';
          $scope._loading.show = false;
        } else if (result.data.length === 0) {
          $scope.flag = '';
          $scope.shoumun.num = 1;
          $scope._loading.show = false;
          $scope.showArrow = true;
          CoreService.dialog('查询无数据');
        } else {
          $scope.flag = '';
          $scope.showArrow = true;
          $scope._loading.show = false;
          CoreService.dialog('错误');
        }
      });
    }
    //切换表格
    $scope.changeTable = function() {
      $scope.flag = 'table';
    };
    //切换图
    $scope.changeView = function() {
      $scope.flag = 'view';
    };
    //省公司排序
    $scope.showSeq = true;
    $scope.seqProvince = function(items) {
      $scope.showSeq = !$scope.showSeq;
      if ($scope.showSeq) {
        items.sort(function(a, b) {
          return Number(a.provinceCode) < Number(b.provinceCode) ? -1 : 1;
        });
      } else {
        items.sort(function(a, b) {
          return Number(a.provinceCode) < Number(b.provinceCode) ? 1 : -1;
        });
      }
    };
    //成功率排序
    $scope.seqSuccessRate = function(items, businessLine) {
      businessLine.successRateArrow = !businessLine.successRateArrow;
      if (businessLine.successRateArrow) {
        items.sort(function(a, b) {
          return Number(a.lineData[0].processSuccessRate) - Number(b.lineData[0].processSuccessRate);
        });
      } else {
        items.sort(function(a, b) {
          return Number(b.lineData[0].processSuccessRate) - Number(a.lineData[0].processSuccessRate);
        });
      }
    };
  }
  srSuccessRateController.$inject = ['$scope', 'successRate', '$timeout', 'CoreService', 'COREDATA','PublicFn'];
}());